package com.example.exceldemo.utils;

import org.apache.poi.ss.usermodel.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class POIUtils {
	
	
	public static final List<String> dateformat = new ArrayList<>(Arrays.asList("交易日期","queryTime"));
	
	/**
	 * @description 删除一行
	 * @param sheet
	 * @param index
	 */
	public static void deleteRow(Sheet sheet,int index){
//		Row row = sheet.getRow(index);
//		sheet.removeRow(row);
		sheet.shiftRows(index, sheet.getLastRowNum(), -1);
	}
	
	
	List<CellStyle> cellStyleList = new ArrayList<>();
	/**
	 * 行复制功能
	 * @param fromRow
	 * @param toRow
	 */
	public static void copyRow(Workbook wb,Row fromRow,Row toRow,boolean copyValueFlag){
//		CellStyle newstyle0 = wb.createCellStyle();
		for (Iterator<Cell> cellIt = fromRow.cellIterator(); cellIt.hasNext();) {
			Cell tmpCell = cellIt.next();
			Cell newCell = toRow.createCell(tmpCell.getColumnIndex());
			copyCell(wb,tmpCell, newCell, copyValueFlag);
		}
	}
	
	/**
	 * 复制单元格
	 * 
	 * @param srcCell
	 * @param distCell
	 * @param copyValueFlag
	 *            true则连同cell的内容一起复制
	 */
	public static void copyCell(Workbook wb,Cell srcCell, Cell distCell,
			boolean copyValueFlag ) {
		//样式
		distCell.setCellStyle(srcCell.getCellStyle());
		//评论
		if (srcCell.getCellComment() != null) {
			distCell.setCellComment(srcCell.getCellComment());
		}
		// 不同数据类型处理
		int srcCellType = srcCell.getCellType();
		distCell.setCellType(srcCellType);
		if (copyValueFlag) {
			if (srcCellType == Cell.CELL_TYPE_NUMERIC) {
				if (DateUtil.isCellDateFormatted(srcCell)) {
					distCell.setCellValue(srcCell.getDateCellValue());
				} else {
					distCell.setCellValue(srcCell.getNumericCellValue());
				}
			} else if (srcCellType == Cell.CELL_TYPE_STRING) {
				distCell.setCellValue(srcCell.getRichStringCellValue());
			} else if (srcCellType == Cell.CELL_TYPE_BLANK) {
				// nothing21
			} else if (srcCellType == Cell.CELL_TYPE_BOOLEAN) {
				distCell.setCellValue(srcCell.getBooleanCellValue());
			} else if (srcCellType == Cell.CELL_TYPE_ERROR) {
				distCell.setCellErrorValue(srcCell.getErrorCellValue());
			} else if (srcCellType == Cell.CELL_TYPE_FORMULA) {
				distCell.setCellFormula(srcCell.getCellFormula());
			} else { // nothing29
			}
		}
	}
	
	/**
	 * @description 复制row
	 * @param sheet
	 * @param rowIndex
	 * @return
	 */
	public static Row createRow(Sheet sheet,Integer rowIndex){
		
		Row  row = null;
		if(sheet.getRow(rowIndex) != null){
			int lastRowNo = sheet.getLastRowNum();
			sheet.shiftRows(rowIndex, lastRowNo, 1);
		}
		row = sheet.createRow(rowIndex);
		return row;
	}
	
}
